<template>
  <div>
    <div v-if="flag == 0">
      <div class="mlt">
        <el-input
          v-model="form.goods_name"
          placeholder="请输入搜索关键字"
          style="width: 200px"
        />
        <el-button style="margin-left: 10px" type="primary" @click="init">
          搜索
        </el-button>
        <el-button
          v-permissions="{ permission: ['add:Inventory'] }"
          @click="addInventor"
        >
          新增盘点单
        </el-button>
        <el-button
          v-permissions="{ permission: ['export:Inventory'] }"
          @click="Export"
        >
          导出
        </el-button>
        <el-button
          v-permissions="{ permission: ['export-view:Inventory'] }"
          type="text"
          @click="exportList"
        >
          查看已导出列表
        </el-button>
      </div>
      <div class="mlt">
        <el-table :data="tableData" :empty-text="emptyText" style="width: 100%">
          <el-table-column label="盘点时间" prop="order_date" />
          <el-table-column label="盘点编号" prop="order_sn" />
          <el-table-column label="盘点结果" prop="order_status_name" />
          <el-table-column label="盘点类型" prop="type_name" />
          <el-table-column label="操作" prop="address">
            <template slot-scope="scoped">
              <el-button
                v-if="scoped.row.order_status != 'wait'"
                v-permissions="{ permission: ['view:Inventory'] }"
                type="primary"
                @click="details(scoped.row)"
              >
                查看
              </el-button>
              <el-button
                v-if="scoped.row.order_status == 'wait'"
                v-permissions="{ permission: ['add:Inventory'] }"
                type="primary"
                @click="goAddInventor(scoped.row)"
              >
                继续完成
              </el-button>
              <el-button
                v-permissions="{ permission: ['delete:Inventory'] }"
                type="danger"
                @click="delinv(scoped.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-if="total >= 10"
          background
          layout="prev, pager, next"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
      <el-dialog title="新增盘点单" :visible.sync="dialogVisible" width="30%">
        <div>
          <span>盘点类型</span>
          <el-select
            v-model="value"
            placeholder="请选择"
            style="margin-left: 10px"
            @change="addGoos"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.type_name"
              :value="item.id"
            />
          </el-select>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
        </span>
      </el-dialog>
      <el-dialog title="导出" :visible.sync="ExportDialogVisible" width="30%">
        <el-table :data="exportData" style="width: 100%">
          <el-table-column label="日期" prop="updated_at" />
          <el-table-column label="操作">
            <template slot-scope="scoped">
              <el-button
                v-permissions="{ permission: ['download:Inventory'] }"
                type="text"
                @click="download(scoped.row)"
              >
                下载
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="ExportDialogVisible = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
    <div v-if="flag == 1">
      <AddInventory :pdata="pdata" @showMain="showMain" />
    </div>
    <div v-if="flag == 2">
      <Detailinstock :detailsdata="detailsdata" @showMain="showMain" />
    </div>
  </div>
</template>

<script>
  import AddInventory from './components/addInventory.vue'
  import Detailinstock from './components/detailinstock.vue'
  import {
    invList,
    invTypeList,
    invMainAdd,
    invDel,
    invExport,
    invLog,
  } from '@/api/commodity'

  //
  export default {
    components: { AddInventory, Detailinstock },

    data() {
      return {
        flag: 0,
        form: {
          page: 1,
          goods_name: '',
          pagesize: 10,
        },
        orderdate: '',
        ordersn: '',
        emptyText: ' ',
        tableData: [],
        dialogVisible: false,
        value: '',
        options: [],
        pdata: {},
        detailsdata: {},
        total: '',
        ExportDialogVisible: false,
        exportData: [],
      }
    },
    mounted() {
      this.init()
    },
    methods: {
      // 导出
      Export() {
        const data = {
          goods_name: this.form.goods_name,
        }
        invExport(data).then(() => {
          // const url = e.data.base_url + e.data.src
          // window.open(url)
          this.$message({
            message: '已添加导出列表',
            type: 'success',
          })
          this.exportList()
        })
      },
      // 导出列表
      exportList() {
        invLog().then((e) => {
          this.exportData = e.data
          this.ExportDialogVisible = true
        })
      },
      download(e) {
        // const url = 'http://fan.jb.cc/' + e.src
        const url = e.base_url + e.src
        window.open(url)
      },
      addInventor() {
        this.value = ''
        this.dialogVisible = true
      },
      addGoos(e) {
        const data = {
          type_id: e,
        }
        invMainAdd(data).then((j) => {
          this.ordersn = j.order_sn
          this.orderdate = j.order_date
          this.pdata = {
            ordersn: j.data.order_sn,
            orderdate: j.data.order_date,
            id: j.data.id,
          }
          this.flag = 1
        })
      },
      details(e) {
        this.detailsdata = {
          id: e.id,
        }
        this.flag = 2
      },
      goAddInventor(e) {
        this.pdata = {
          ordersn: e.order_sn,
          orderdate: e.order_date,
          id: e.id,
        }
        this.flag = 1
      },
      delinv(e) {
        this.$confirm('此操作将永久删除该盘点单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const data = {
              id: e.id,
            }
            invDel(data).then(() => {
              this.init()
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
      },
      showMain(e) {
        this.flag = e
        this.dialogVisible = false
        this.$nextTick(() => {
          this.init()
        })
      },
      init() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        // const data = {
        //   pagesize: 10,
        //   page: this.form.page,
        // }
        invList(this.form).then((e) => {
          this.tableData = e.data.data
          this.total = e.data.total
          loading.close()
          if (e.data.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
        invTypeList().then((e) => {
          this.options = e.data
        })
      },
      handleCurrentChange(val) {
        const _this = this
        _this.form.page = val
        this.init()
      },
    },
  }
</script>

<style></style>
